//index.js
//获取应用实例
var app = getApp();
var util = require('../../../utils/util.js');
Page({
  data: {
    statusType: ["计划中", "已放鱼", "已结束", "已取消"],
    currentType: 0,
    tabClass: ["", "", "", ""],
    dataList: null,
    page: 1
  },
  statusTap: function(e) {
    var curType = e.currentTarget.dataset.index;
    this.data.currentType = curType
    this.setData({
      currentType: curType
    });
    this.onShow();
  },

  onLoad: function(e) {

  },
  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      page: 1
    })
    that.onShow();
  },
  onReachBottom: function() {

  },
  onShow: function(e) {
    // 获取订单列表
    wx.showLoading();
    var that = this;
    var postData = {
      token: app.globalData.token,
    };
    var _page = that.data.page;
    postData.status = that.data.currentType;
    postData.page = _page;
    wx.request({
      url: app.globalData.Domain + '/fish/mylist',
      data: postData,
      success: (res) => {
        wx.hideLoading();
        if (res.data.code == 0) {
          var list = res.data.data;
          for (var i = 0; i < list.length; i++) {
            var item = list[i];
            item.put_time_format = util.formatTime(item.put_time, 'd');
          }
          that.setData({
            dataList: res.data.data
          });
        } else {
          this.setData({
            dataList: null
          });
        }
      },
      complete: (res) => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    })
  },
  dataDetailTap: function(e) {
    var _id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "../../fish/detail?id=" + _id
    })
  },
  addtap: function(e) {
    wx.navigateTo({
      url: "add"
    })
  },
  editTap: function(e) {
    var _id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: "add?id=" + _id
    })
  },
  cancelTap: function(e) {
    var that = this;
    var fishid = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确定要取消该放鱼计划吗？',
      content: '',
      cancelText: '看错了',
      confirmText: '确定取消',
      success: function(res) {
        if (res.confirm) {
          wx.showLoading();
          wx.request({
            method: 'POST',
            url: app.globalData.Domain + '/fish/cancel',
            data: {
              token: app.globalData.token,
              id: fishid
            },
            success: (res) => {
              if (res.data.code == 0) {
                that.onShow();
              }
              else{
                wx.showModal({
                  title: '哪有鱼提示',
                  content: res.data.msg
                })
              }
            },
            complete: () => {
              wx.hideLoading();
            }
          })
        }
      }
    })
  },
  toPutFish: function(e) {
    var _id = e.currentTarget.dataset.id;
    var _title = e.currentTarget.dataset.title;
    var _field_title = e.currentTarget.dataset.field_title;
    wx.navigateTo({
      url: "putfish?id=" + _id + "&title=" + _title + "&f_title=" + _field_title
    })
  },
})